//
//  GSubSectionView.m
//  helpinrain
//
//  Created by Gang.Wang on 12-8-19.
//  Copyright (c) 2012年 Zhuhai Yuike Technology Co., Ltd. All rights reserved.
//

#import "GSubSectionView.h"
#import "mcf.h"
#import "GSheetXmlModel.h"
#import "GStepXmlModel.h"
#import "GStepView.h"


@interface GSubSectionView ()
{
    UILabel * nameLabel_;
    NSMutableArray * stepViews_;
}

@property (nonatomic, strong) UILabel * nameLabel;
@property (nonatomic, strong) NSMutableArray * stepViews;

- (void) privateInit;
- (void) showDataToView;

@end

@implementation GSubSectionView

@synthesize subSection = subSection_;
@synthesize nameLabel = nameLabel_;

@synthesize stepViews = stepViews_;

- (void) dealloc
{
    YRelease(subSection_)
    YRelease(nameLabel_)
    
    YRelease(stepViews_)
    
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}

- (void) setSubSection:(GSubSection *)subSectionValue
{
    YRelease(subSection_)
    subSection_ = [subSectionValue retain];
    
    [self showDataToView];
    [self reAdjustLayout];
}

- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    self.stepViews = [NSMutableArray array];
    
    
    UILabel * temp_label = [[UILabel alloc] init];
    [temp_label setFont:[UIFont systemFontOfSize:isPad ? 16 : 14]];
    self.nameLabel = temp_label;
    YRelease(temp_label)
    
    [self addSubview:self.nameLabel];
    
    self.bInited = TRUE;
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self reAdjustLayout];
}

- (void) showDataToView
{
    if (!self.subSection)
    {
        return;
    }
    
    [self.nameLabel setText:self.subSection.name];
    
    for (GStepXmlModel * stepModel in self.subSection.steps)
    {
        GStepView * stepView = [[GStepView alloc] init];
        [stepView setStepData:stepModel];
        
        [self addSubview:stepView];
        [self.stepViews addObject:stepView];
        YRelease(stepView)
    }
    [self reAdjustLayout];
}

- (float) needHeight
{
    float result = 50;
    for (GStepView * stepView in self.stepViews)
    {
        result += [stepView needHeight];
        result += 20;
    }
    
    
    
    return result;
}

- (void) reAdjustLayout
{
    [self.nameLabel setFrame:CGRectMake(0, 0, self.width, isPad ? 50 : 30)];
    float origin_y__ = isPad ? 52 : 32;
    for (GStepView * stepView in self.stepViews)
    {
        float height = [stepView needHeight];
        [stepView setFrame:CGRectMake(isPad? 15 : 5, origin_y__, self.width - (isPad ? 30 : 10), height)];
        
        origin_y__ += height + 20;
    }
   
}

@end
